var Promise = require('bluebird');
var config = require('mongoose').model('Config');

module.exports = {
  update: function (query, info) {
    var defer = Promise.defer();
    config.update(query, info).exec(function (err, data) {
      if (err) {
        return defer.reject('update error')
      } else {
        defer.resolve({status: true, message: data})
      }
    });
    return defer.promise
  },
  all: function () {
    var defer = Promise.defer();
    config.where().exec(function (err, data) {
      if (err) {
        defer.reject('find all station error')
      } else {
        defer.resolve(data)
      }
    });
    return defer.promise;
  },
  create: function (data) {
    var defer = Promise.defer();
    config.where({$or: [{recorder: data.recorder}]}).exec()
      .then(function (result) {
        if (result.length === 0) {
          config.create(data, function (err, data) {
            if (err) {
              return defer.reject('创建失败')
            }
            defer.resolve({status: true, message: data})
          });
        } else {
          defer.resolve({status: false, message: '已存在'})
        }
      });
    return defer.promise;
  }
};